import axios from 'axios'
import {ServiceConfig,SGSConfig } from './app-config.js';
import querystring  from 'querystring';
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
axios.defaults.headers.put['Content-Type'] = 'application/x-www-form-urlencoded';
axios.defaults.withCredentials = true;
let casurl = SGSConfig.ticket;
//新增项目详情
export const addProjectDetail = (params) =>{
    return axios.post(ServiceConfig.projectDetailUrl,params);
}
//修改项目详情
export const updateProjectdetail = (params) =>{
    return axios.put(ServiceConfig.projectDetailUrl,params);
}
//删除项目详情
export const deleteProjectdetail = (pkid) =>{
    return axios.delete(ServiceConfig.projectDetailUrl + "/" + pkid);
}
//根据pkid获取项目详情
export const queryProjectdetailByPkid = (pkid) =>{
    return axios.get(ServiceConfig.projectDetailUrl + "/" + pkid);
}
//查找项目详情
export const queryProjectdetail = (params) => {
    for(var key in params){
        if(params[key]==""){
            params[key]==null;
        }
    }
    return axios.get(ServiceConfig.projectDetailsUrl,{params:params});
}
//新增项目年进度
export const addProYearSchedule = (params) => {
    return axios.post(ServiceConfig.proYearScheduleUrl,params);
}
//修改项目年进度
export const updateProYearSchedule = (params) => {
    return axios.put(ServiceConfig.proYearScheduleUrl,params);
}
//删除项目年进度
export const deleteProYearSchedule = (pkid) =>{
    return axios.delete(ServiceConfig.proYearScheduleUrl + "/" + pkid);
}
//根据进度id获取年项目进度
export const queryProYearScheduleByPkid = (pkid) =>{
    return axios.get(ServiceConfig.proYearScheduleUrl + "/" + pkid);
}
//根据项目id获取项目年进度
export const queryProYearScheduleByProjectid = (pkid,params) =>{
    return axios.get(ServiceConfig.proYearSchedulesUrl + "/" + pkid,{params:params});
}
//新增项目月进度
export const addProMonthSchedule = (params) => {
    return axios.post(ServiceConfig.proMonthScheduleUrl,params);
}
//修改项目月进度
export const updateProMonthSchedule = (params) => {
    return axios.put(ServiceConfig.proMonthScheduleUrl,params);
}
//删除项目月进度
export const deleteProMonthSchedule = (pkid) =>{
    return axios.delete(ServiceConfig.proMonthScheduleUrl + "/" + pkid);
}
//根据进度id获取月项目进度
export const queryProMonthScheduleByPkid = (pkid) =>{
    return axios.get(ServiceConfig.proMonthScheduleUrl + "/" + pkid);
}
//根据项目id获取项目月进度
export const queryProMonthScheduleByProjectid = (pkid,params) =>{
    return axios.get(ServiceConfig.proMonthSchedulesUrl + "/" + pkid,{params:params});
}
//新增项目多媒体
export const addProjectMedia = (params) =>{
    return axios.post(ServiceConfig.projectMediaUrl,params);
}
//修改项目多媒体
export const updateProjectMedia = (params) =>{
    return axios.post(ServiceConfig.updateProjectMediaUrl,params);
}
//删除项目多媒体
export const deleteProjectMedia = (pkid) =>{
    return axios.delete(ServiceConfig.projectMediaUrl + "/" + pkid);
}
//根据id获取多媒体
export const queryProjectMediaByPkid = (pkid) =>{
    return axios.get(ServiceConfig.projectMediaUrl + '/' + pkid);
}
//根据进度id获取多媒体
export const queryProjectMediaByScheduleid = (pkid,params) =>{
    return axios.get(ServiceConfig.projectMediasUrl + '/' + pkid,{params:params});
}
//获取多媒体文件
export const getArchiveFile = (pkid) =>{
    return axios.get(ServiceConfig.archiveUrl + "/" + pkid);
}
//导出项目
export const exportProjects = (pkids) =>{
    return window.location.href = ServiceConfig.exportProjectsUrl+"?pkids=" + pkids;
}
//导入项目
export const uploadExcelData = (params) =>{
    return axios.post(ServiceConfig.uploadExcelDataUrl,params);
}

//查询某新闻分类下的新闻
export const queryNewsClass = (pkid,params) =>{
    return axios.get(SGSConfig.getNewsClass + "/" + pkid+".json",{params:params});
}
//新闻详情
export const queryNewsDetail = (pkid) =>{
    return axios.get(SGSConfig.getNews + "/" + pkid+".json");
}


/**
 * cas登录注销接口
 */
export const setCookie = (name, value, domain, path, hour) =>{
    let expire = new Date();
    expire.setTime(expire.getTime() + (hour?3600000 * hour:30*24*60*60*1000));
    let tmpCookie = name + "=" + value + "; " + "expires=" + expire.toGMTString()+"; path="+ (path ? path :"/")+ "; "  + (domain ? ("domain=" + domain + ";") : "");
    document.cookie = tmpCookie;
}
const getCookie = (name) => {
    var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));
    if(arr != null) return unescape(arr[2]);
    return null;
}
const deleteCookie =  (name, domain, path) => {
    var tmpCookie = name + "=; expires=Mon, 26 Jul 1997 05:00:00 GMT; path="+ (path ? path :"/")+ "; " + (domain ? ("domain=" + domain + ";") : "");
    document.cookie = tmpCookie;
}

const deleteTGT = (casTGC) =>{
    return axios.delete(`${casurl}/${casTGC}`, {withCredentials: true});
}

//注销
export const logoutToLogin = () => {
    var casTGC = getCookie("CASTGCTMP");
    deleteCookie("CASTGCTMP");
    return deleteTGT(casTGC);
}

export const apiGetUserInfo = (options) => {
    return axios.get(`${SGSConfig.getUser}.json`, {withCredentials: true});
}

export const apiLogin = ({username, password}) => {
    return axios.post(casurl,querystring.stringify({
        username: username,
        password: password
    }))
}

export const apiSTForSite = (TGT, site) => {
    return axios.post(`${SGSConfig.ticket}/${TGT}`,querystring.stringify({
        service:site
    }))
}

export const apiCasCall = (site, ST) => {
    return axios({
        method: 'get',
        url: site,
        params: {'ticket': ST},
    })
}
//根据id查找地图服务
export const getMapById = (id) => {
    return  axios.get(ServiceConfig.getMapByIdUrl+"/"+id+".json");
}
//根据父模块id查找子模块
export const getModulesById = (id) => {
    return axios.get(ServiceConfig.getModulesByIdUrl,{params:{pkid:id}});
}
//获取地图
export const getMap = () => {
    return axios.get(ServiceConfig.getMap)
}
//导入月报
export const uploadMonthData = (params) =>{
    return axios.post(ServiceConfig.uploadMonthData,params)
}
//根据时间查找月报
export const queryProMonthScheduleByTime = (params) =>{
    return axios.get(ServiceConfig.queryProMonthScheduleByTime,{params:params})
}
//导出月报
export const exportMonthData = (params) =>{
    return window.location.href = ServiceConfig.exportMonthData+"?"+"dateMonth="+params.dateMonth;
}

